package com.hzit.controller;

import com.hzit.config.StudentConverter;
import com.hzit.dto.StudentDTO;
import com.hzit.service.IClassesService;
import com.hzit.service.IStudentService;
import com.hzit.vo.StudentVO;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IClassesService classesService;
	//1. 查询学生列表
	@GetMapping("/list")
	public String list(Model model){
		//1.1 查询返回学生列表
		List<StudentDTO> studentDTOS = studentService.findAll();
		//1.2 将学生对象放到model中
		model.addAttribute("students",studentDTOS);
		//1.3 查询出班级
		model.addAttribute("classes",classesService.findAll());
		//1.4 返回视图
		return "list";
	}

	//2. 添加学生
	@PostMapping("/add")
	public String add(StudentVO vo){
		//2.1 将vo转换为DTO传递到service层
		StudentDTO studentDTO = StudentConverter.INSTANCE.vo2Dto(vo);
		//2.2 再将dto添加到数据
		studentService.addStudent(studentDTO);
		//2.3 跳转到列表页面
		return "redirect:/student/list";
	}

	//3. 删除学生
	@GetMapping("delete")
	public String delete(Integer sid){
		studentService.delete(sid);
		return "redirect:/student/list";
	}
}
